﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using YunSpace.Data;
using YunSpace.Data.Extensions;
using YunSpace.Domain.Entity.WorkFlow;
using YunSpace.Domain.IRepository.WorkFlow;
#endregion

namespace YunSpace.Repository.WorkFlow
{
    //public class FormDesignerRepository : RepositoryBase<FormDesignerEntity>, IFormDesignerRepository
    public class FormDesignerRepository : IFormDesignerRepository
    {
        private MySqlDbHelper dbHelper = new MySqlDbHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormDesignerRepository()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">FormDesignerEntity实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(FormDesignerEntity model)
        {
            string sql = @"INSERT INTO WorkFlow_FormDesigner
				(F_Id,F_Name,F_Type,F_CreatorUserId,F_CreatorUserName,F_CreatorTime,F_LastModifyTime,F_Html,F_SubTableJson,F_EventsJson,F_Attribute,F_Status) 
				VALUES(@ID,@Name,@Type,@CreateUserID,@CreateUserName,@CreateTime,@LastModifyTime,@Html,@SubTableJson,@EventsJson,@Attribute,@Status)";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id },
                new MySqlParameter("@Name", MySqlDbType.VarChar, 500){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 50){ Value = model.F_Type },
                new MySqlParameter("@CreateUserID", MySqlDbType.VarChar, 50){ Value = model.F_CreatorUserId },
                new MySqlParameter("@CreateUserName", MySqlDbType.VarChar, 100){ Value = model.F_CreatorUserName },
                new MySqlParameter("@CreateTime", MySqlDbType.DateTime, 8){ Value = model.F_CreatorTime },
                new MySqlParameter("@LastModifyTime", MySqlDbType.DateTime, 8){ Value = model.F_LastModifyTime },
                model.F_Html == null ? new MySqlParameter("@Html", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Html", MySqlDbType.Text, -1) { Value = model.F_Html },
                model.F_SubTableJson == null ? new MySqlParameter("@SubTableJson", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@SubTableJson", MySqlDbType.Text, -1) { Value = model.F_SubTableJson },
                model.F_EventsJson == null ? new MySqlParameter("@EventsJson", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@EventsJson", MySqlDbType.Text, -1) { Value = model.F_EventsJson },
                model.F_Attribute == null ? new MySqlParameter("@Attribute", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@Attribute", MySqlDbType.VarChar, -1) { Value = model.F_Attribute },
                new MySqlParameter("@Status", MySqlDbType.Int32){ Value = model.F_Status }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">FormDesignerEntity实体类</param>
        public int Update(FormDesignerEntity model)
        {
            string sql = @"UPDATE WorkFlow_FormDesigner SET 
				F_Name=@Name,F_Type=@Type,F_CreatorUserId=@CreateUserID,F_CreatorUserName=@CreateUserName,F_CreatorTime=@CreateTime,F_LastModifyTime=@LastModifyTime,F_Html=@Html,F_SubTableJson=@SubTableJson,F_EventsJson=@EventsJson,F_Attribute=@Attribute,F_Status=@Status
				WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@Name", MySqlDbType.VarChar, 500){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 50){ Value = model.F_Type },
                new MySqlParameter("@CreateUserID", MySqlDbType.VarChar, 50){ Value = model.F_CreatorUserId },
                new MySqlParameter("@CreateUserName", MySqlDbType.VarChar, 100){ Value = model.F_CreatorUserName },
                new MySqlParameter("@CreateTime", MySqlDbType.DateTime, 8){ Value = model.F_CreatorTime },
                new MySqlParameter("@LastModifyTime", MySqlDbType.DateTime, 8){ Value = model.F_LastModifyTime },
                model.F_Html == null ? new MySqlParameter("@Html", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Html", MySqlDbType.Text, -1) { Value = model.F_Html },
                model.F_SubTableJson == null ? new MySqlParameter("@SubTableJson", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@SubTableJson", MySqlDbType.Text, -1) { Value = model.F_SubTableJson },
                model.F_EventsJson == null ? new MySqlParameter("@EventsJson", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@EventsJson", MySqlDbType.Text, -1) { Value = model.F_EventsJson },
                model.F_Attribute == null ? new MySqlParameter("@Attribute", MySqlDbType.VarChar, -1) { Value = DBNull.Value } : new MySqlParameter("@Attribute", MySqlDbType.VarChar, -1) { Value = model.F_Attribute },
                new MySqlParameter("@Status", MySqlDbType.Int32){ Value = model.F_Status },
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(string id)
        {
            string sql = "DELETE FROM WorkFlow_FormDesigner WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<FormDesignerEntity> DataReaderToList(MySqlDataReader dataReader)
        {
            List<FormDesignerEntity> List = new List<FormDesignerEntity>();
            FormDesignerEntity model = null;
            while (dataReader.Read())
            {
                model = new FormDesignerEntity();
                model.F_Id = dataReader.GetString(0);
                model.F_Name = dataReader.GetString(1);
                model.F_Type = dataReader.GetString(2);
                model.F_CreatorUserId = dataReader.GetString(3);
                model.F_CreatorUserId = dataReader.GetString(4);
                model.F_CreatorTime = dataReader.GetDateTime(5);
                model.F_LastModifyTime = dataReader.GetDateTime(6);
                if (!dataReader.IsDBNull(7))
                    model.F_Html = dataReader.GetString(7);
                if (!dataReader.IsDBNull(8))
                    model.F_SubTableJson = dataReader.GetString(8);
                if (!dataReader.IsDBNull(9))
                    model.F_EventsJson = dataReader.GetString(9);
                if (!dataReader.IsDBNull(10))
                    model.F_Attribute = dataReader.GetString(10);
                model.F_Status = dataReader.GetInt32(11);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<FormDesignerEntity> GetAll()
        {
            string sql = "SELECT * FROM WorkFlow_FormDesigner";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<FormDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM WorkFlow_FormDesigner";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public FormDesignerEntity Get(string id)
        {
            string sql = "SELECT * FROM WorkFlow_FormDesigner WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = id }
            };
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<FormDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }



        /// <summary>
        /// 查询一个分类所有记录
        /// </summary>
        public List<FormDesignerEntity> GetAllByType(string types)
        {
            string sql = "SELECT * FROM WorkFlow_FormDesigner where F_Type IN(" + Utility.Tools.GetSqlInString(types) + ")";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<FormDesignerEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}

